﻿module Sustainalytics.Event {

    import Common = Sustainalytics.Common;

    // EventViewController controller
    export class EventViewController {
        public eventsCurrentDate: EventDto[];
        public orderBy: string[];
        public orderAscending: boolean;
        public startDate: string;
        public currentUTCDate: Date;
        public disablePreviousButton: boolean;

        //Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private $routeParams: ng.route.IRouteParamsService,
            private EventService: EventService,
            private notificationFactory: Common.INotificationFactory) {

            var ctx = this;

            ctx.$routeParams = $routeParams;
            $scope['eventView'] = ctx;

            var date = new Date();
            ctx.currentUTCDate = ctx.toUTCDate(date);

            var curentDate: string = ctx.$routeParams['eventsDate'];
            curentDate = curentDate.replace("\"", "");
            curentDate = curentDate.substring(0, 10);

            var dateForDisablePrevButton = new Date(curentDate);
            dateForDisablePrevButton.setDate(dateForDisablePrevButton.getDate() - 1);

            ctx.disablePreviousButton = true;

            var diffMonth = ctx.monthDiff(ctx.currentUTCDate, dateForDisablePrevButton);

            if (diffMonth < -2) {
                ctx.disablePreviousButton = true;
            }
            else {
                ctx.disablePreviousButton = false;
            }

            ctx.getEventsByDate(curentDate);

        }

        public toUTCDate = function (date) {
            var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
            return _utc;
        };

        public getEventsByDate(startDate: string): ng.IPromise<EventDto> {
            var ctx = this;
            ctx.$rootScope['loading'] = true;

            ctx.startDate = startDate;

            // get event by id
            var promiseGet: ng.IPromise<EventDto> = ctx.EventService.getEventByDate(ctx.startDate);

            //on success
            promiseGet
                .then(function (lst) {
                    ctx.eventsCurrentDate = lst["items"];
                })
                ['finally'](function () {
                    ctx.$rootScope['loading'] = false;
                });
            return promiseGet;
        }

        public getNextDate() {
            var ctx = this;
            ctx.$rootScope['loading'] = true;

            //convert string to date
            var currentDate = ctx.startDate.substring(0,10);
            var newDate = new Date(currentDate);

            newDate.setDate(newDate.getDate() + 1);

            ctx.startDate = newDate.getFullYear() + "-" + ('0' + (newDate.getMonth() + 1)).slice(-2) + "-" + ('0' + newDate.getDate()).slice(-2);

            ctx.$routeParams['eventsDate'] = ctx.startDate;
            ctx.$location.path.apply(ctx.$location, ["events/date/" + ctx.startDate]);
        }

        public getPreviousDate() {
            var ctx = this;
            ctx.$rootScope['loading'] = true;

            //convert string to date
            var currentDate = ctx.startDate.substring(0, 10);
            var newDate = new Date(currentDate);

            newDate.setDate(newDate.getDate() - 1);

            ctx.startDate = newDate.getFullYear() + "-" + ('0' + (newDate.getMonth() + 1)).slice(-2) + "-" + ('0' + newDate.getDate()).slice(-2);

            ctx.$routeParams['eventsDate'] = ctx.startDate;
            ctx.$location.path.apply(ctx.$location, ["events/date/" + ctx.startDate]);
        }

        public monthDiff(d1, d2) {
            var months;
            months = (d2.getFullYear() - d1.getFullYear()) * 12;
            months -= d1.getMonth();
            months += d2.getMonth();
            return months <= 0 ? months : months;
        }
    }

    // register controller 
    angular.module('Sustainalytics.Event').controller('EventViewController', function ($rootScope: ng.IScope, $scope: ng.IScope, $q: ng.IQService, $location: ng.ILocationService, $routeParams: ng.route.IRouteParamsService, EventService, notificationFactory: Common.INotificationFactory) {
        var ctrl = new Sustainalytics.Event.EventViewController($rootScope, $scope, $q, $location, $routeParams, EventService, notificationFactory);
        return ctrl;
    });
}
 